<?php
	/**
	 * @package AmpleHarvest
	 */
	require_once 'TableHeader.php';

	/**
	 * @brief This class Describes the Query_History Table in the AmpleHarvest Database
	 *
	 * - Revision History
	 *  - 0.1
	 * 	 - <b>Date</b>: 02 JUN 2011
	 * 	 - <b>Author</b>: Randy Blancett
	 * 	  - Initial Version
	 *
	 * @author Randy Blancett
	 * @version 0.1
	 * @ingroup code classes table
	 */
	class cTABLE_Data extends cTable
	{
		/**
		 * @{
		 * @name Status
		 */
		const C_INT_STATUS_INDEX_FAIL = -2;
		const C_INT_STATUS_STAGE_1_FAIL = -1;
		const C_INT_STATUS_NEW = 0;
		const C_INT_STATUS_PROCESSING_DATA = 1;
		const C_INT_STATUS_STAGE_1_SUCCESS = 2;
		const C_INT_STATUS_INDEXING_DATA = 3;
		const C_INT_STATUS_INDEX_SUCCESS = 4;
		/**
		 * @}
		 */

		/**
		 * @{
		 * @name Columns
		 */
		public $m_int_COL_Category = null;
		public $m_int_COL_Data = null;
		public $m_int_COL_DataToken = null;
		public $m_int_COL_HarvestDate = null;
		public $m_int_COL_ID = null;
		public $m_int_COL_IndexDate = null;
		public $m_int_COL_IndexToken = null;
		public $m_int_COL_OrigID = null;
		public $m_int_COL_Page = null;
		public $m_int_COL_ProcessToken = null;
		public $m_int_COL_PublishDate = null;
		public $m_int_COL_Source = null;
		public $m_int_COL_Status = null;

		/**
		 * @}
		 */

		/**
		 * This constructor sets all the required column data
		 */
		public function __construct()
		{
			$this->m_str_TableName = "data";
			$this->set_Engine(self::C_STR_ENGINE_INNODB);
			$this->set_CharSet(self::C_STR_CHAR_SET_BINARY);
			//$this->set_Collation(self::C_STR_COLLATION_BINARY);

			//Create Column Objects
			$obj_COL_ID = new cColVarChar("id");
			$obj_COL_ID->set_Length(36);
			$obj_COL_ID->set_Index(abs_Column::c_int_INDEX_PRIMARY);
			$obj_COL_ID->set_Comment("This column holds the ID of the Data.");
			$obj_COL_ID->set_Editable(false);
			$obj_COL_ID->set_Label("ID");

			$obj_COL_Category = new cColVarChar("category");
			$obj_COL_Category->set_Length(50);
			$obj_COL_Category->set_Index(abs_Column::c_int_INDEX_INDEX);
			$obj_COL_Category->set_Comment("This column holds the category of the data.");
			$obj_COL_Category->set_Label("Category");

			$obj_COL_Data = new cColLongBlob("data");
			$obj_COL_Data->set_Comment("This column holds the data.");
			$obj_COL_Data->set_Label("Data");

			$obj_COL_DataToken = new cColVarChar("dataToken");
			$obj_COL_DataToken->set_Length(32);
			$obj_COL_DataToken->set_Index(abs_Column::c_int_INDEX_INDEX);
			$obj_COL_DataToken->set_Comment("This is the MD5 hash code of the last data update.");
			$obj_COL_DataToken->set_Label("Data Token");

			$obj_COL_HarvestDate = new cColDateTime("harvest_date");
			$obj_COL_HarvestDate->set_Index(abs_Column::c_int_INDEX_INDEX);
			$obj_COL_HarvestDate->set_Comment("This column holds the date the data was harvested.");
			$obj_COL_HarvestDate->set_Label("Harvest Date");

			$obj_COL_IndexDate = new cColDateTime("index_date");
			$obj_COL_IndexDate->set_Comment("This column holds the date the data was indexed.");
			$obj_COL_IndexDate->set_Label("Index Date");

			$obj_COL_IndexToken = new cColVarChar("index_token");
			$obj_COL_IndexToken->set_Length(32);
			$obj_COL_IndexToken->set_Index(abs_Column::c_int_INDEX_INDEX);
			$obj_COL_IndexToken->set_Comment("This is the value the data token was set to when the data was indexed.");
			$obj_COL_IndexToken->set_Label("Index Token");

			$obj_COL_OrigID = new cColVarChar("orig_id");
			$obj_COL_OrigID->set_Length(100);
			$obj_COL_OrigID->set_Index(abs_Column::c_int_INDEX_INDEX);
			$obj_COL_OrigID->set_Comment("This column holds the id this data had from the original source.");
			$obj_COL_OrigID->set_Label("Original ID");

			$obj_COL_Page = new cColTinyInt("page");
			$obj_COL_Page->set_Comment("This is the page number of the document.");
			$obj_COL_Page->set_Index(abs_Column::c_int_INDEX_INDEX);
			$obj_COL_Page->set_Label("Page");

			$obj_COL_ProcessToken = new cColVarChar("processToken");
			$obj_COL_ProcessToken->set_Length(32);
			$obj_COL_ProcessToken->set_Index(abs_Column::c_int_INDEX_INDEX);
			$obj_COL_ProcessToken->set_Comment("This is the value the data token was set to when the data was processed.");
			$obj_COL_ProcessToken->set_Label("Data Token");

			$obj_COL_PublishDate = new cColDateTime("publish_date");
			$obj_COL_PublishDate->set_Comment("This column holds the date the data was published.");
			$obj_COL_PublishDate->set_Label("Publish Date");

			$obj_COL_Source = new cColVarChar("source");
			$obj_COL_Source->set_Length(200);
			$obj_COL_Source->set_Comment("This column holds the source of the data.");
			$obj_COL_Source->set_Index(abs_Column::c_int_INDEX_INDEX);
			$obj_COL_Source->set_Label("Source");

			$obj_COL_Status = new cColTinyInt("status");
			$obj_COL_Status->set_Comment("This column holds the status of the data.");
			$obj_COL_Status->set_Index(abs_Column::c_int_INDEX_INDEX);
			$obj_COL_Status->set_Label("Status");

			//Set up connection
			$this->m_obj_Connection = new cConnection("AmpleHarvest", "admin");

			//Activate all Debug options
			$this->m_obj_Connection->set_DebugLevel(cLog_Connection_Debug::c_int_LEVEL_HIDDEN);
			$this->m_obj_Connection->set_LogLevel(cLog_Connection::c_int_LEVEL_BASIC);
			//$this->m_obj_Connection->activate_Logging();
			//$this->m_obj_Connection->activate_LogDebug();
			//$this->m_obj_Connection->activate_ScreenDebug();

			$this->m_int_COL_ID = $this->add_Column($obj_COL_ID);
			$this->m_int_COL_Category = $this->add_Column($obj_COL_Category);
			$this->m_int_COL_Data = $this->add_Column($obj_COL_Data);
			$this->m_int_COL_DataToken = $this->add_Column($obj_COL_DataToken);
			$this->m_int_COL_HarvestDate = $this->add_Column($obj_COL_HarvestDate);
			$this->m_int_COL_IndexDate = $this->add_Column($obj_COL_IndexDate);
			$this->m_int_COL_IndexToken = $this->add_Column($obj_COL_IndexToken);
			$this->m_int_COL_OrigID = $this->add_Column($obj_COL_OrigID);
			$this->m_int_COL_Page = $this->add_Column($obj_COL_Page);
			$this->m_int_COL_ProcessToken = $this->add_Column($obj_COL_ProcessToken);
			$this->m_int_COL_PublishDate = $this->add_Column($obj_COL_PublishDate);
			$this->m_int_COL_Source = $this->add_Column($obj_COL_Source);
			$this->m_int_COL_Status = $this->add_Column($obj_COL_Status);
		}

		public function insert_DefaultData()
		{

			return true;
		}

		/**
		 * Returns if the id is a valid and existing ID
		 * @param string $str_User_ID ID of the Page to check (GUID)
		 * @return boolean true if valid false if not
		 */
		public function validate_ID($str_ID)
		{
			// ensure that there is an ID passed and that it is a valid GUID
			if (!empty($str_ID) && preg_match(self::c_REG_GUID_VALID, $str_ID))
			{
				$this->clear_Query();
				$this->add_ColumnQuery($this->m_int_COL_ID, $str_ID);
				if ($this->do_Search() > 0)
				{
					return true;
				}
			}
			return false;
		}

		/**
		 * Loads information into object from Database based on ID passed
		 * @param string $str_ID ID of the id to check (int)
		 * return boolean true if loaded false if not
		 */
		public function load_DataFromID($str_ID)
		{
			$this->clear();

			if ($this->validate_ID($str_ID))
			{
				$this->clear_Query();
				$this->add_ColumnQuery($this->m_int_COL_ID, $str_ID);
				if ($this->do_Search() > 0)
				{
					$obj_RS = $this->get_ResultSet();
					if ($obj_RS[0])
					{
						$this->load_DataFromArray($obj_RS[0]);
						return true;
					}
				}
			}
			return false;
		}

		static public function encode($str_Data)
		{
			$str_Data = base64_encode(serialize($str_Data));
			return $str_Data;
		}

		static public function decode($str_Data)
		{
			$str_Data = unserialize(base64_decode($str_Data));
			return $str_Data;
		}

		/**
		 * Saves Information to the Database
		 */
		public function save_Data2DB()
		{
			if ($this->get_data())
			{
				$this->set_dataToken(md5($this->get_data()));
			}

			if ($this->validate_ID($this->get_id()))
			{
				return $this->update_Data2DB();
			}
			else
			{
				return $this->insert_Data2DB();
			}
		}
		/*
		 public function get_data()
		 {
		 return utf8_decode($this->m_arr_Variables["data"]);
		 }
														
		 public function set_data($str_Data)
		 {
		 $this->m_arr_Variables["data"] = utf8_encode($str_Data);
		 }
		 */
		/**
		 * This function is used to update data that is already in the database
		 */

		public function update_Data2DB()
		{
			//			$this->clear_Update();
			//			$this->add_ColumnQuery($this->m_int_COL_ID, $this->get_id());

			if (!$this->validate_Update())
			{
				return false;
			}
			foreach ($this->m_arr_Variables as $key => $data)
			{
				switch ($key)
				{

					case $this->get_Column_Name($this->m_int_COL_Data):
						if ($data)
						{
							$this->add_Update($key, $data);

							//$this->add_Update($key, "0x".$this->strToHex($data),true);
						}
						break;

					default:
						$this->add_Update($key, $data);
				}
			}
			$int_Return = $this->do_Update();
			if ($int_Return < 0)
			{
				return false;
			}
			return true;
		}

		/**
		 * This function is used to insert Data into the database
		 */

		public function insert_Data2DB()
		{
			//			$this->set_id($this->create_GUID());
			//			$this->set_date(date_format(new datetime(), "Y-m-d H:i:s"));
			if (!$this->validate_New())
			{
				return false;
			}
			$this->clear_Insert();
			foreach ($this->m_arr_Variables as $key => $data)
			{
				switch ($key)
				{

					case $this->get_Column_Name($this->m_int_COL_Data):
						if ($data)
						{
							$this->add_Insert($key, $data);
							//$this->add_Insert($key, "0x".$this->strToHex($data),true);
						}
						break;

					default:
						$this->add_Insert($key, $data);
				}
			}
			$int_Return = $this->do_Insert();
			if ($int_Return < 0)
			{
				return false;
			}
			return true;
		}

		private function strToHex($string)
		{
			//$arr_UTF8 = utf8ToUnicode($string);
			$hex = '';
			//foreach($arr_UTF8 as $int_Code)
			//{
			//	$hex .= dechex($int_Code);
			//}

			for ($i = 0; $i < strlen($string); $i++)
			{
				$hex .= dechex(ord($string[$i]));
			}

			return $hex;
		}

	}
?>
